package org.huqiwen.blog.logic;

import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import org.huqiwen.blog.dao.DataSource;
import org.huqiwen.blog.data.BlogDataBean;
import org.huqiwen.blog.data.CategoryDataBean;
/**
 * 文章分类操作处理类
 * @author Administrator
 *
 */
public class CategoryLogicBean {
	private int allRows;	//总记录数
	private int curPage=1;	//当前页
	private int pageSize=5; //每页显示的数据条数
	private int allPage;	//总页数
	
	public int getCurPage() {
		return curPage;
	}
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getAllRows() {
		return allRows;
	}
	public int getAllPage() {
		return allPage;
	}
	/**
	 * 获得分类列表
	 * @return
	 */
	public List<CategoryDataBean> findAllCategory(){
		List<CategoryDataBean> list = new ArrayList<CategoryDataBean>();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			stmt= DataSource.getConnection().createStatement();
			rs = stmt.executeQuery("select * from category");
			while(rs.next()){
				CategoryDataBean bean = new CategoryDataBean();
				bean.setCatId(rs.getString(1));
				bean.setCatName(rs.getString(2));
				list.add(bean);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			try {
				if (null!=stmt)stmt.close();			
				if (null!=rs) rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return list;
	} 
	/**
	 * 根据参数catId，查找相应的分类下面的所有文章,返加类型为文章列表
	 * @param catId
	 * @return 文章列表
	 */
	public List<BlogDataBean> findBlogByCat(String catId){
		List<BlogDataBean> list = new ArrayList<BlogDataBean>();
		Statement stmt = null;
		ResultSet rs = null;
		try {
			String sql = "select id,post_title,post_content,post_date,cat_name,post_status,comment_status,post_modified,post_count"
						+" from posts,category"
						+" where posts.post_category = category.cat_id and cat_name ="+"'"+catId+"'";

			stmt = DataSource.getConnection().createStatement(ResultSet.CONCUR_READ_ONLY,ResultSet.CONCUR_READ_ONLY);
			rs = stmt.executeQuery(sql);
			rs.last();	//移动到最后一条记录；
			this.allRows = rs.getRow();//得到记录的总条数
			if (this.allRows%this.pageSize == 0) {
				this.allPage = this.allRows/this.pageSize;
			}else {
				this.allPage = this.allRows/this.pageSize + 1;
			}
			
			if (this.curPage ==1) {
				rs.beforeFirst();	//如果是第一页，将指针移动到记录的第一列
			}else {
				rs.absolute((this.curPage-1)*this.pageSize);//否则移动到当前页的第一条记录
			}
			
			int i = 0;
			while(rs.next()&&i<this.pageSize){	//将从数据中查询到数据存储到list中	
				BlogDataBean bean = new BlogDataBean();
				bean.setBlogId(rs.getString(1));
				bean.setBlogTitle(rs.getString(2));
				bean.setBlogContent(rs.getString(3));
				bean.setPostData(rs.getString(4));
				bean.setBlogCategory(rs.getString(5));
				bean.setBlogStatus(rs.getString(6));
				bean.setCommentSatatus(rs.getString(7));
				bean.setBlogModdified(rs.getString(8));
				bean.setBlogCount(rs.getString(9));	
				list.add(bean);
				i++;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			if (null!=stmt) {
				try {
					stmt.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
			if (null!=rs) {
				try {
					rs.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
		return list;
	}
}
